package project2.ui.game.operate;

import project2.entity.enemy.EnemyPlane;
import project2.resources.Sounds;
import project2.ui.game.GamePlayingPanel;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class UseBomb extends MouseAdapter {

    private final GamePlayingPanel panel;

    public UseBomb(GamePlayingPanel panel) {
        this.panel = panel;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (panel.myPlane.isAlive() && panel.myPlane.getHoldBombCount() > 0) {
            Sounds.useBombSoundPlayer.play();// 使用炸弹的声音
            useBomb();
        }
    }

    public void useBomb() {
        Graphics g = panel.getGameGraphics();// 面板的画图板

        for (EnemyPlane enemyPlane : panel.enemyPlanes) {// 所有敌机都要销毁
            panel.score.addAndGet(enemyPlane.getKilledScore());
            switch (enemyPlane.getEnemyType()) {// 敌机销毁声音
                case SMALL_ENEMY_PLANE:
                    Sounds.smallPlaneKilledSoundPlayer.play();
                    break;
                case BIG_ENEMY_PLANE:
                    Sounds.bigPlaneKilledSoundPlayer.play();
                    break;
                case BOSS_ENEMY_PLANE:
                    Sounds.bossPlaneKilledSoundPlayer.play();
                    break;
            }
            enemyPlane.drawKilled(g);// 销毁敌机
        }

        // 面板中全部元素清空
        panel.bullets.clear();
        panel.enemyPlanes.clear();
        panel.popBomb = null;
        panel.popDoubleLaser = null;
        panel.myPlane.getHoldBombList().remove(0);

        panel.repaint();// 面板重绘
    }

}
